package glory.action;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import glory.bean.Glory;
import glory.factory.GloryFactory;
import glory.service.GloryService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class GloryAction extends ActionSupport implements SessionAware{
	private Glory Glory=new Glory();
	private List<Glory> Glorylist=new ArrayList<Glory>();
	private Map session;
	private File image;    //file to upload 
	private String imageFileName; //file name
	private String imageContentType;  //file type
	private InputStream inputStream;
	private String fileName;
	private int pagenow;
	
	public int getPagenow(){
		return pagenow;
	}
    public  void setPagenow(int pagenow){
    	this.pagenow=pagenow;
    }
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	 public File getImage() {
	        return image;
	    }

	    public void setImage(File img) {
	        this.image = img;
	    }

	    public String getImageFileName() {
	        return imageFileName;
	    }

	    public void setImageFileName(String imageFileName) {
	        this.imageFileName = imageFileName;
	    }

	    public String getImageContentType() {
	        return imageContentType;
	    }

	    public void setImageContentType(String imageContentType) {
	        this.imageContentType = imageContentType;
	    }


	public void setGlory(Glory Glorybean){
		this.Glory=Glorybean;
	}
	
	public Glory getGlory(){
		return Glory;
	}
	
	public List<Glory> getGlorylist(){
		return Glorylist;
	}
	
	public void setGlorylist(List<Glory> Glorylist){
		this.Glorylist=Glorylist;
	}
	
	public Map getSession() {
		return session;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	
	public String modifyGlory() throws Exception {
		if(Glory.getTitle().trim().equalsIgnoreCase("")){
			this.addActionError("modifyGlory error");
			return INPUT;
		}
	   if(Glory.getContent().trim().equalsIgnoreCase("")){
		   this.addActionError("modifyGlory error");
		   return INPUT;
	   }

		GloryService Gloryservice=GloryFactory.getInstance().creatGloryService();
        if(!Gloryservice.ModifyGlory(Glory)){
        	this.addActionError("modifyGlory error");
        	return INPUT;
        }
		
		Map session = ActionContext.getContext().getSession();
		session.remove("Glory");
		return SUCCESS;
	}
	
	public String deleteGlory() throws Exception {
		GloryService Gloryservice =GloryFactory.getInstance().creatGloryService();
		if (!Gloryservice.DeleteGlory(Glory)) {
			this.addActionError("deleteGlory error");
			return INPUT;
		} else {
			this.addActionError("deleteGlory error");
			return SUCCESS;
		}
	}
	
	public String addGlory() throws Exception {	
		if(image!=null){
		Random rnd = new Random();
		int p = rnd.nextInt(20);
		String num = Integer.toString(p);
		String path=ServletActionContext.getServletContext().getRealPath("/");
		path=path.replace("bestcleaner", "images"); 
		imageFileName = num + "." + imageFileName;
		File file = new File(path + "//" + imageFileName);
		imageFileName="/images/"+imageFileName;
		FileUtils.copyFile(image, file);		
		}
		else 
			imageFileName="";
		GloryService Gloryservice=GloryFactory.getInstance().creatGloryService();
		if (Glory.getTitle().trim().equalsIgnoreCase("")) {
			this.addActionError("addGlory error");
			return INPUT;
		}
		if (Glory.getContent().trim().equalsIgnoreCase("")) {
			this.addActionError("addGlory error");
			return INPUT;
		}
		if(!Gloryservice.AddGlory(Glory,imageFileName)){
			this.addActionError("addGlory error");
			return INPUT;
		}
		this.addActionError("addGlory error");
		return SUCCESS;
	}

	 public String showallGlory() throws Exception{
		 GloryService Gloryservice=GloryFactory.getInstance().creatGloryService();
		 Glorylist=Gloryservice.ShowAllGlory(getPagenow(),6);
		 if(getPagenow()==0){
			 int total=Gloryservice.CountAllGlory();
			 int page=0;
			 do {
					total = total - 6;
					page++;
				} while (total > 0);
				this.session.put("glorytotalpage", page);
		 }
		 if(Glorylist.size()==0){
			 this.addActionError("showallGlory error");
			return INPUT;
		 }else{
			 return  SUCCESS; 
		 }
	 }
	 
	 public String showoneGlory() throws Exception{
			GloryService GloryService =GloryFactory.getInstance().creatGloryService();
			Glory= GloryService.ShowoneGlory(Glory);
			if (Glory == null) {
				this.addActionError("Error!");
				return INPUT;
			} else {
				this.session.put("Glory", Glory);
				return SUCCESS;
			}
		}
	 }

